package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.StatsStoreDeviceTypeDao;
import com.huilian.iotbox.admin.wap.service.CommodityStockRecordService;
import com.huilian.iotbox.admin.wap.service.DeviceService;
import com.huilian.iotbox.admin.wap.service.MerchantStoreService;
import com.huilian.iotbox.admin.wap.service.StatsStoreDeviceTypeService;
import com.huilian.iotbox.data.dto.*;
import com.huilian.iotbox.data.enums.CommodityStockRecordFirstTypeEnum;
import com.huilian.iotbox.data.enums.CommodityStockRecordSecondTypeEnum;
import com.huilian.iotbox.data.enums.ConsumeRecordTypeEnum;
import com.huilian.iotbox.data.enums.DeviceTypeEnum;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: loong
 * @create: 2023-04-19 20:41
 **/
@Slf4j
@Service
public class StatsStoreDeviceTypeServiceImpl implements StatsStoreDeviceTypeService {
    @Resource
    private StatsStoreDeviceTypeDao statsStoreDeviceTypeDao;
    @Resource
    private CommodityStockRecordService commodityStockRecordService;


    //初始化 百分比 0
    private void initStoreDevicePercent(StatsStoreDeviceTypeVo statsStoreDeviceTypeVo){
        BigDecimal money = new BigDecimal(0);
        statsStoreDeviceTypeVo.setPercentCashMoney(money);
        statsStoreDeviceTypeVo.setPercentActivityTurnover(money);
        statsStoreDeviceTypeVo.setPercentTurnoverOnLine(money);
        statsStoreDeviceTypeVo.setPercentTurnoverMeiTuan(money);
        statsStoreDeviceTypeVo.setPercentTurnoverDouYin(money);
    }
    private void amountStoreDevicePercent(StatsStoreDeviceTypeVo statsStoreDeviceTypeVo){
        this.initStoreDevicePercent(statsStoreDeviceTypeVo);
        if (statsStoreDeviceTypeVo.getTurnover().compareTo(BigDecimal.ZERO) > 0) {
            ArrayList<BigDecimal> percentList = new ArrayList<>();
            //现金收款 除以 总营业额 保留两位小数 乘 100
            BigDecimal percentCashMoney = statsStoreDeviceTypeVo.getCashMoney().divide(statsStoreDeviceTypeVo.getTurnover(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
            statsStoreDeviceTypeVo.setPercentCashMoney(percentCashMoney);
            percentList.add(percentCashMoney);
            // 活动总结算额 除以 总营业额 保留两位小数 乘 100
            BigDecimal activityTurnover1 = statsStoreDeviceTypeVo.getActivityTurnover().divide(statsStoreDeviceTypeVo.getTurnover(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
            statsStoreDeviceTypeVo.setPercentActivityTurnover(activityTurnover1);
            percentList.add(activityTurnover1);

            // 线上营业额  除以 总营业额 保留两位小数 乘 100
            BigDecimal percentTurnoverOnLine = statsStoreDeviceTypeVo.getTurnoverOnLine().divide(statsStoreDeviceTypeVo.getTurnover(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
            statsStoreDeviceTypeVo.setPercentTurnoverOnLine(percentTurnoverOnLine);
            percentList.add(percentTurnoverOnLine);

            //抖音收款
            BigDecimal percentTurnoverDouYin = statsStoreDeviceTypeVo.getTurnoverDouYin().divide(statsStoreDeviceTypeVo.getTurnover(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
            statsStoreDeviceTypeVo.setPercentTurnoverDouYin(percentTurnoverDouYin);
            percentList.add(percentTurnoverDouYin);

            //美团收款
            BigDecimal percentTurnoverMeiTuan = statsStoreDeviceTypeVo.getTurnoverMeiTuan().divide(statsStoreDeviceTypeVo.getTurnover(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
            statsStoreDeviceTypeVo.setPercentTurnoverMeiTuan(percentTurnoverMeiTuan);
            percentList.add(percentTurnoverMeiTuan);
            BigDecimal maxPercent = Collections.max(percentList);
            BigDecimal bfb = new BigDecimal(100);
            //0是现金，1是活动...
            Integer index = -1;
            boolean flog = true;
            if(maxPercent.compareTo(BigDecimal.ZERO) == 1){
                for(int i = 0; i < percentList.size(); i++){
                    if(percentList.get(i).compareTo(maxPercent) == 0 && flog){
                        //记录最大百分比坐标
                        index = i;
                        flog = false;
                        continue;
                    }
                    bfb = bfb.subtract(percentList.get(i));
                }
                switch (index){
                    case 0:
                        statsStoreDeviceTypeVo.setPercentCashMoney(bfb);
                        break;
                    case 1:
                        statsStoreDeviceTypeVo.setPercentActivityTurnover(bfb);
                        break;
                    case 2:
                        statsStoreDeviceTypeVo.setPercentTurnoverOnLine(bfb);
                        break;
                    case 3:
                        statsStoreDeviceTypeVo.setPercentTurnoverDouYin(bfb);
                        break;
                    case 4:
                        statsStoreDeviceTypeVo.setPercentTurnoverMeiTuan(bfb);
                        break;
                }
            }
        }
    }
    @Override
    public List<StatsStoreDeviceTypeVo> findOrderStatisticsGroupByDeviceType(StatsDeviceDto statsDeviceDto) {
        List<StatsStoreDeviceTypeVo> returnStatsStoreDeviceTypeList = new ArrayList<>();
        List<StatsStoreDeviceTypeVo> statsStoreDeviceTypeVoList = statsStoreDeviceTypeDao.findOrderStatisticsGroupByDeviceType(statsDeviceDto);
        // 使用Map集合可以避免双重循环提高效率
        Map<String, StatsStoreDeviceTypeVo> statsStoreDeviceTypeVoMap = statsStoreDeviceTypeVoList.stream().collect(Collectors.toMap(StatsStoreDeviceTypeVo::getDeviceNo, v -> v));
        for (DeviceVo deviceVo : statsDeviceDto.getDeviceVoList()) {
            log.info("设备编号{}：",deviceVo.getDeviceNo());
            StatsStoreDeviceTypeVo storeDeviceTypeVo = statsStoreDeviceTypeVoMap.get(deviceVo.getDeviceNo());
            if(ObjectUtils.isEmpty(storeDeviceTypeVo)){
                //初始化数据
                storeDeviceTypeVo = new StatsStoreDeviceTypeVo();
                this.initStoreDeviceTypeData(storeDeviceTypeVo);
            }
            storeDeviceTypeVo.setDeviceNo(deviceVo.getDeviceNo());
            storeDeviceTypeVo.setAlias(deviceVo.getAlias());
            storeDeviceTypeVo.setDeviceTypeName(deviceVo.getDeviceTypeName());
            storeDeviceTypeVo.setDeviceTypeId(deviceVo.getDeviceTypeId());
            StatsStoreDeviceTypeDto statsDeviceStoreDto = new StatsStoreDeviceTypeDto();
            statsDeviceStoreDto.setUserInfo(statsDeviceDto.getUserInfo());
            statsDeviceStoreDto.setDeviceTypeId(deviceVo.getDeviceTypeId());
            statsDeviceStoreDto.setMchStoreId(statsDeviceDto.getMchStoreId());
            statsDeviceStoreDto.setDeviceNo(deviceVo.getDeviceNo());
            statsDeviceStoreDto.setDeviceId(deviceVo.getId());
            statsDeviceStoreDto.setStartDateStr(statsDeviceDto.getStartDateStr());
            statsDeviceStoreDto.setEndDateStr(statsDeviceDto.getEndDateStr());

            //售货机统计
            storeDeviceTypeVo.setTotalQuantity(0);
            if(deviceVo.getDeviceTypeId().compareTo(DeviceTypeEnum.VENDING_MACHINE.getCode())==0 ||
                    deviceVo.getDeviceTypeId().compareTo(DeviceTypeEnum.SCREEN_VENDING_MACHINE.getCode())==0 ||
            deviceVo.getDeviceTypeId().compareTo(DeviceTypeEnum.OPEN_DOOR_VENDING_MACHINE.getCode())==0){
                CommodityStockRecordDto commodityStockRecordDto = new CommodityStockRecordDto();
                commodityStockRecordDto.setUserInfo(statsDeviceDto.getUserInfo());
                commodityStockRecordDto.setMerchantStoreId(statsDeviceDto.getMchStoreId());
                commodityStockRecordDto.setSecondaryType(CommodityStockRecordSecondTypeEnum.VENDING_MACHINE_OUT.getCode());
                commodityStockRecordDto.setFirstType(CommodityStockRecordFirstTypeEnum.OUTBOUND.getCode());
                commodityStockRecordDto.setStartTime(DateTimeUtils.getTodayStartTime());
                commodityStockRecordDto.setEndTime(DateTimeUtils.getTodayEndTime());
                commodityStockRecordDto.setDeviceNo(deviceVo.getDeviceNo());
                CommodityStockRecordVo commodityStockRecordVo = commodityStockRecordService.findCommodityStockRecordSumByDeviceNo(commodityStockRecordDto);
                storeDeviceTypeVo.setTotalQuantity(commodityStockRecordVo.getTotalQuantity());
            }

            if(statsDeviceDto.getDayType() == 0 || statsDeviceDto.getDayType() == 1){
                this.outConStoreDevicePercentV2(storeDeviceTypeVo);
                this.amountStoreDevicePercent(storeDeviceTypeVo);
            }
            returnStatsStoreDeviceTypeList.add(storeDeviceTypeVo);
        }
        return returnStatsStoreDeviceTypeList;
    }
  private  StatsStoreDeviceTypeVo outConStoreDevicePercentV2(StatsStoreDeviceTypeVo statsStoreDeviceTypeVo){
        //初始化
        BigDecimal rate = new BigDecimal(0);
        statsStoreDeviceTypeVo.setPercentOffLinePayOutCoin(rate);
        statsStoreDeviceTypeVo.setPercentFetchMemberOutCoin(rate);
        statsStoreDeviceTypeVo.setPercentOnLinePayOutCoinTotal(rate);
        statsStoreDeviceTypeVo.setPercentOnLinePayOutCoinTotal(rate);
        statsStoreDeviceTypeVo.setPercentArtificialOutCoin(rate);
        statsStoreDeviceTypeVo.setPercentMeiTuanOutCoin(rate);
        statsStoreDeviceTypeVo.setPercentDouYinOutCoin(rate);
        statsStoreDeviceTypeVo.setPercentWeiXinVerifyOutCoin(rate);
        statsStoreDeviceTypeVo.setPercentInsertCoinOffline(rate);
        statsStoreDeviceTypeVo.setPercentInsertCoinOnLine(rate);
        statsStoreDeviceTypeVo.setPercentDistributeBenefitCoin(rate);
        statsStoreDeviceTypeVo.setPercentCoinRedPacketCoin(rate);
        //购币情况
        if(statsStoreDeviceTypeVo.getOutCoinTotal() > 0){
            BigDecimal total = new BigDecimal(statsStoreDeviceTypeVo.getOutCoinTotal());
            // 现金出币 除以 总出币 保留两位小数 乘 100
            BigDecimal offLinePayOutCoinTotal = new BigDecimal(statsStoreDeviceTypeVo.getOffLinePayOutCoinTotal());
            BigDecimal offLinePayOutCoinTotalRate = offLinePayOutCoinTotal.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentOffLinePayOutCoin(offLinePayOutCoinTotalRate.multiply(new BigDecimal("100")));

            //线上出币
            BigDecimal linePayOutCoinTotal = new BigDecimal(statsStoreDeviceTypeVo.getOnLinePayOutCoinTotal());
            BigDecimal linePayOutCoinTotalRate = linePayOutCoinTotal.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentOnLinePayOutCoinTotal(linePayOutCoinTotalRate.multiply(new BigDecimal("100")));

            //会员取币
            BigDecimal fetchMemberOutCoinTotal = new BigDecimal(statsStoreDeviceTypeVo.getFetchMemberOutCoinTotal());
            BigDecimal fetchMemberOutCoinTotalRate = fetchMemberOutCoinTotal.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentFetchMemberOutCoin(fetchMemberOutCoinTotalRate.multiply(new BigDecimal("100")));

            //抖音核销出币
            BigDecimal douYinOutCoinTotal = new BigDecimal(statsStoreDeviceTypeVo.getDouYinOutCoinTotal());
            BigDecimal douYinOutCoinTotalRate = douYinOutCoinTotal.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentDouYinOutCoin(douYinOutCoinTotalRate.multiply(new BigDecimal("100")));

            //美团核销出币
            BigDecimal meiTuanOutCoinTotal = new BigDecimal(statsStoreDeviceTypeVo.getMeiTuanOutCoinTotal());
            BigDecimal meiTuanOutCoinTotalRate = meiTuanOutCoinTotal.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentMeiTuanOutCoin(meiTuanOutCoinTotalRate.multiply(new BigDecimal("100")));

            //微信核销出币
            BigDecimal weiXinVerifyOutCoinTotal = new BigDecimal(statsStoreDeviceTypeVo.getWeiXinVerifyTotalCoin());
            BigDecimal weiXinVerifyOutCoinTotalRate = weiXinVerifyOutCoinTotal.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentWeiXinVerifyOutCoin(weiXinVerifyOutCoinTotalRate.multiply(new BigDecimal("100")));

        }

        //投币情况
        if(statsStoreDeviceTypeVo.getInsertCoinTotal() > 0){
            BigDecimal total = new BigDecimal(statsStoreDeviceTypeVo.getInsertCoinTotal());
            //线上投币
            BigDecimal insertCoinOnLine = new BigDecimal(statsStoreDeviceTypeVo.getInsertCoinOnLine());
            BigDecimal insertCoinOnLineRate = insertCoinOnLine.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentInsertCoinOnLine(insertCoinOnLineRate.multiply(new BigDecimal("100")));

            //线下投币
            BigDecimal insertCoinOffline = new BigDecimal(statsStoreDeviceTypeVo.getInsertCoinOffline());
            BigDecimal insertCoinOfflineRate = insertCoinOffline.divide(total, 2, BigDecimal.ROUND_HALF_UP);
            statsStoreDeviceTypeVo.setPercentInsertCoinOffline(insertCoinOfflineRate.multiply(new BigDecimal("100")));
        }
        return statsStoreDeviceTypeVo;
    }
    private void initStoreDeviceTypeData(StatsStoreDeviceTypeVo storeDeviceTypeVo) {
        storeDeviceTypeVo.setTurnover(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setSettlementMoney(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setCashMoney(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setTurnoverOnLine(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setActivityTurnover(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setOutCoinTotal(0);
        storeDeviceTypeVo.setOnLineOutCoinTotal(0);
        storeDeviceTypeVo.setOnLinePayOutCoinTotal(0);
        storeDeviceTypeVo.setOffLinePayOutCoinTotal(0);
        storeDeviceTypeVo.setInsertCoinTotal(0);
        storeDeviceTypeVo.setInsertCoinOnLine(0);
        storeDeviceTypeVo.setInsertCoinOffline(0);
        storeDeviceTypeVo.setGiftConsume(0);
        storeDeviceTypeVo.setGiftCost(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setGiftOutRate(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setTurnoverDouYin(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setTurnoverMeiTuan(BigDecimal.valueOf(0));
        storeDeviceTypeVo.setFetchMemberOutCoinTotal(0);
        storeDeviceTypeVo.setMeiTuanOutCoinTotal(0);
        storeDeviceTypeVo.setTotalQuantity(0);
        storeDeviceTypeVo.setDouYinOutCoinTotal(0);
        storeDeviceTypeVo.setWeiXinVerifyTotalCoin(0);
        storeDeviceTypeVo.setWeiXinSeckillOutCoinTotal(0);
        storeDeviceTypeVo.setWeiXinGroupBuyingOutCoinTotal(0);
        storeDeviceTypeVo.setWeiXinVoucherOutCoinTotal(0);
        storeDeviceTypeVo.setArtificialOutCoinTotal(0);
        storeDeviceTypeVo.setGameCoinRedPacketCoinTotal(0);
        storeDeviceTypeVo.setGroupBenefitCoinTotal(0);
        storeDeviceTypeVo.setDistributeBenefitCoinTotal(0);
        storeDeviceTypeVo.setQuantityOfCoinsSold(0);
        storeDeviceTypeVo.setAmountOfCoinsConsumed(0);
        storeDeviceTypeVo.setRemainingCoins(0);
        storeDeviceTypeVo.setAverageCoinPrice(BigDecimal.ZERO);
        storeDeviceTypeVo.setTurnoverGameCoin(BigDecimal.ZERO);
        storeDeviceTypeVo.setScanCodePayTotalTurnover(BigDecimal.ZERO);
        storeDeviceTypeVo.setScanningFee(BigDecimal.ZERO);
        storeDeviceTypeVo.setTiktokCancellationFee(BigDecimal.ZERO);
        storeDeviceTypeVo.setMeiTuanCancellationFee(BigDecimal.ZERO);
    }
}